package com.cc.controller;

import com.cc.model.EbUser;
import com.cc.service.UserService;
import com.cc.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@Controller
@RequestMapping("/user")
public class UserController {

        @Autowired
        private UserService userService;
        @RequestMapping("/list")
        public ModelAndView list(@RequestParam(value = "page",defaultValue = "1") int page,
                                 @RequestParam(value = "pageSize",defaultValue = "3")int pageSize){
            //获取新闻表的第一页数据，每页三条
            List<EbUser> user=userService.list(page,pageSize);
            int maxPage = userService.getMaxPage(pageSize);
            //将数据（list）和视图（jsp）返回给中央控制器spring mvc
            ModelAndView mv=new ModelAndView();
            //绑定数据
            mv.addObject("user",user);//key-value
            //绑定当前页
            mv.addObject("curPage",page);
            //绑定最大页
            mv.addObject("maxPage",maxPage);
            //绑定视图
            mv.setViewName("manage/user");
            return mv;

        }
    @RequestMapping("/to_save")
    public String toSave(){

            return "manage/user-add";
    }



    @RequestMapping("/save_commit")
    public String saveCommit(EbUser ebUser){
        System.out.println("ebUser:"+ebUser);
        //补充添加时间
        //获取系统时间
        Date date = new Date();
        //Date  --->  String
        SimpleDateFormat sdf  = new SimpleDateFormat("yyyy-MM-dd");
        String timeStr = sdf.format(date);
        //System.out.println("timeStr=="+timeStr);
        ebUser.setEuBirthday(timeStr);
        //1.添加数据到数据库
        //1.1参数校验
        if((ebUser.getEuUserId()==null||
                ebUser.getEuUserId().equals("")||
                ebUser.getEuUserId().trim().length()==0)
                ||(ebUser.getEuUserName()==null||
                ebUser.getEuUserName().equals("")||
                ebUser.getEuUserName().trim().length()==0)||
                (ebUser.getEuPassword()==null||
                        ebUser.getEuPassword().equals("")||
                        ebUser.getEuPassword().trim().length()==0)||
                (ebUser.getEuSex()==null||
                        ebUser.getEuSex().equals("")||
                        ebUser.getEuSex().trim().length()==0)
        ){
            return "manage/manage-error-user";
        }
        //2
    int result = userService.save(ebUser);
        if(result<=0){
            return "manage/error";//异常
        }
        return "manage/manage-result";
    }

    @RequestMapping("/to_login")
    public String toLogin(){
            return "login";
    }

    @RequestMapping("/login")
    public String login(String userName, String passWord, HttpServletRequest request){
            System.out.println("userName="+userName+"\tpassWord="+passWord);
        HttpSession session = request.getSession();
        session.removeAttribute("lgn_error");
        //1.查找数据库是否存在这个用户
        //1.1 得到密码的密文
        String md5Pwd = MD5Util.MD5EncodeUtf8(passWord);
        //1.2
        EbUser loginUser =  userService.checkUser(userName,md5Pwd);
        System.out.println("loginUser:==="+loginUser);

        if(loginUser==null){
            session.setAttribute("lgn_error","登录失败");
            //重定向
            return "redirect:to_login";
        }
        //2.将视图告诉spring mvc中处理器
        //2.1 要将用户信息放到session
        session.setAttribute("lgnUser",loginUser);
        //跳转到网站的前台首页
        return "index";
    }





@RequestMapping("/delete/{id}")
  public String delete(@PathVariable("id")String id){
            int result = userService.deleteByPK(id);
        if (result == 0) {

    }
    return  "manage/manage-result";
}
    @RequestMapping("/edit")
    public String edit(Model model, String euUserId) {
        EbUser ebUser = userService.selectByCondition(euUserId);
        model.addAttribute("ebUser", ebUser);
        System.out.println(ebUser);
        return "manage/user-modify";
    }
    @RequestMapping("/editSubmit")
    public String editSubmit(EbUser ebUser){
        int result=userService.updateById(ebUser);
        if (result == 0) {
            return  "manage/error";
        }
        return  "manage/manage-result";
    }
    @RequestMapping("/to_register")
    public String toRegister(){
        return "register";
    }
    @RequestMapping("/reg_commit")
    public String reg_commit(EbUser ebUser,String euUserId, int birthYear,int birthMonth,int birthDay,HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.removeAttribute("reg_error");
        if ((ebUser.getEuUserId() == null ||
                ebUser.getEuUserId().equals("") ||
                ebUser.getEuUserId().trim().length() == 0)
                || (ebUser.getEuUserName() == null ||
                ebUser.getEuUserName().equals("") ||
                ebUser.getEuUserName().trim().length() == 0) ||
                (ebUser.getEuPassword() == null ||
                        ebUser.getEuPassword().equals("") ||
                        ebUser.getEuPassword().trim().length() == 0) ||
                (ebUser.getEuSex() == null ||
                        ebUser.getEuSex().equals("") ||
                        ebUser.getEuSex().trim().length() == 0)
        ) {
            return "redirect:to_register";
        }
        EbUser regUser = userService.checkUser2(euUserId);
        if (regUser != null) {
            session.setAttribute("reg_error", "注册失败");
            //重定向
            return "redirect:to_register";
        }
        session.setAttribute("regUser", regUser);

        //密码进行加密
        String md5pwd = MD5Util.MD5EncodeUtf8(ebUser.getEuPassword());
        ebUser.setEuPassword(md5pwd);

        Date date = new Date(birthYear, birthMonth, birthDay);
        //Date  --->  String
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String timeStr = sdf.format(date);
        //System.out.println("timeStr=="+timeStr);
        ebUser.setEuBirthday(timeStr);

        //调用service
        int result = userService.save(ebUser);
        //2、将添加是否成功打视图告诉spring mvc中央控制器
        if (result <= 0) {
            //异常
        }
        return "reg-result";
    }

}



